import { useEffect, } from "react"
import { useSearchParams } from "react-router-dom"
import { getQuestionList } from "../service"
import { KEYWORD,PAGENAME,PAGESIZNAME ,DEFAULT_PAGESIZE} from "../concats"

import { useRequest } from "ahooks"

type optType = {
    start:boolean,
    trash:boolean
}


const useLoadQuestionList = (type:optType)=>{
    const {start,trash} = type
    const [params] = useSearchParams()

    const {run,data,loading,refresh} = useRequest(()=>{
        const keyword = params.get(KEYWORD) || ''
        const page = parseInt(params.get(PAGENAME) || '') || 1
        const pagesize = parseInt(params.get(PAGESIZNAME) || '') || DEFAULT_PAGESIZE
        return getQuestionList({keyword,page,pagesize,start,trash})
    },{
        manual:true,
    })

    useEffect(()=>{
        run()
    },[params])

    return {
        data,loading,refresh
    }

}

export default useLoadQuestionList